package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.*;
import com.guigu.pojo.*;
import com.guigu.pojo.WarehouseInfo;
import com.guigu.service.WarehouseDetailsInfoService;
import com.guigu.service.WarehouseInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseInfoServiceimpl extends ServiceImpl<WarehouseInfoMapper, WarehouseInfo> implements WarehouseInfoService {
    @Autowired
    WarehouseInfoMapper warehouseInfoMapper;//仓库表
    @Autowired
    WarehouseDetailsInfoMapper warehouseDetailsInfoMapper;//仓库详情表
    @Autowired
    WarehouseClassificationMapper warehouseClassificationMapper;//仓库商品分类表
    @Autowired
    ShopTypeInfoMapper shopTypeInfoMapper;//商品分类表
    @Autowired
    ShopInfoMapper shopInfoMapper;//商品表
    //添加仓库
    @Override
    public Map addWarehouseInfo(WarehouseInfo warehouseInfo) {
        Map<String,Object> map=new HashMap<>();
        //设置属性
        //设置为等待审核
        warehouseInfo.setIsCheck(0);
        //设置为未删除
        warehouseInfo.setIsDelete(0);
        //进行添加
        if(warehouseInfoMapper.insert(warehouseInfo)>0){
            map.put("msg","添加成功，等待审核");
            map.put("x",true);
            //获取仓库商品分类集合，循环添加到仓库商品分类表内
            for(WarehouseClassification w: warehouseInfo.getWarehouseClassificationList()){
                w.setPId(warehouseInfo.getId());
                warehouseClassificationMapper.insert(w);
            }
        }else{
            map.put("msg","操作失败！");
            map.put("x",false);
        }
        return map;
    }

    //审核仓库
    @Override
    public Map checkWarehouseInfo(WarehouseInfo warehouseInfo) {
        Map<String,Object> map=new HashMap<>();
        //检查本次操作是否是拒绝通过
        if(warehouseInfo.getIsCheck()==2){
            //拒绝通过，那么就删除仓库商品分类表对应仓库id的数据
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("p_id",warehouseInfo.getId());
            warehouseClassificationMapper.delete(queryWrapper);
            //进行修改
            if(warehouseInfoMapper.updateById(warehouseInfo)>0){
                map.put("msg","拒绝通过！");
                map.put("x",true);
            }else{
                map.put("msg","操作失败！");
                map.put("x",false);
            }
        }else {
            //不是拒绝通过
            //进行修改
            if(warehouseInfoMapper.updateById(warehouseInfo)>0){
                map.put("msg","允许通过！");
                map.put("x",true);
            }else{
                map.put("msg","操作失败！");
                map.put("x",false);
            }
        }
        return map;
    }

    @Override
    public Page<WarehouseInfo> queryAllShops(WarehouseInfo warehouseInfo, Integer pageno, Integer pagesize) {
        QueryWrapper<WarehouseInfo> queryWrapper=new QueryWrapper<WarehouseInfo>();
        //条件查询
        //审核状态
        if (warehouseInfo.getIsCheck()!=null){
            queryWrapper.eq("is_check",warehouseInfo.getIsCheck());
        }
        //删除状态
        if (warehouseInfo.getIsCheck()!=null){
            queryWrapper.eq("is_delete",warehouseInfo.getIsDelete());
        }
        Page<WarehouseInfo> page = this.page(new Page<WarehouseInfo>(pageno, pagesize), queryWrapper);
        //循环集合，补全属性
        for(WarehouseInfo w : page.getRecords()){
            //查询仓库商品分类表
            QueryWrapper queryWrapper2=new  QueryWrapper<WarehouseClassification>();
            queryWrapper2.eq("p_id",w.getId());
            List<WarehouseClassification> warehouseClassificationList=warehouseClassificationMapper.selectList(queryWrapper2);
            //循环仓库商品分类表，补全属性
            String allWarehouseClassificationName="";
            for(WarehouseClassification warehouseClassification : warehouseClassificationList){
                warehouseClassification.setName(shopTypeInfoMapper.selectById(warehouseClassification.getShopClassId()).getName());
                //拼接分类名称
                allWarehouseClassificationName+=warehouseClassification.getName()+",";
            }
            w.setAllWarehouseClassificationName(allWarehouseClassificationName.substring(0,allWarehouseClassificationName.length()-1));
            //计算剩余库存
            w.setRemnantInventory(calculateRemnantInventory(w));
        }
        return page;
    }

    //获取指定商品可以存放的仓库有哪些
    @Override
    public List<WarehouseInfo> assignWarehouseInfo(ShopInfo shopInfo) {
        List<WarehouseInfo> result=new ArrayList<>();
        //补全商品属性
        shopInfo=shopInfoMapper.selectById(shopInfo.getId());
        //获取仓库表集合
        QueryWrapper queryWrapper=new QueryWrapper<WarehouseInfo>();
        //条件查询
        //审核通过
        queryWrapper.eq("is_check","1");
        //未被删除
        queryWrapper.eq("is_delete","0");
        List<WarehouseInfo> warehouseInfoList=warehouseInfoMapper.selectList(queryWrapper);
        //循环仓库表，进行数据筛选
        if(warehouseInfoList.size()>0){
            for(WarehouseInfo w : warehouseInfoList){
                //检查仓库表的仓库分类表数据是否是商品表的分类数据是否相同
                queryWrapper=new QueryWrapper<WarehouseClassification>();
                queryWrapper.eq("p_id",w.getId());
                List<WarehouseClassification> warehouseClassificationList=warehouseClassificationMapper.selectList(queryWrapper);
                for(WarehouseClassification ware : warehouseClassificationList){
                    //检查分类id是否相同
                    if(ware.getShopClassId() == shopInfo.getShopType()){
                        //只查询商品id等于当前要存放的商品id
                        QueryWrapper queryWrapper2=new QueryWrapper<WarehouseDetailsInfo>();
                        queryWrapper2.eq("shop_id",shopInfo.getId());
                        queryWrapper2.eq("p_id",w.getId());
                        //根据商品id，查询仓库详情表
                        if(!(warehouseDetailsInfoMapper.selectList(queryWrapper2).size()>0)){
                            //没有查询到数据，说明该仓库未配置过该商品，添加到集合内
                            result.add(w);
                        }
                    }
                }
            }
        }
        return result;
    }

    //根据id，获取仓库对象
    @Override
    public WarehouseInfo queryWarehouseInfoById(Integer id) {
        WarehouseInfo warehouseInfo=warehouseInfoMapper.selectById(id);
        //计算剩余库存
        warehouseInfo.setRemnantInventory(calculateRemnantInventory(warehouseInfo));
        //计算最大可分配商品库存数量
        warehouseInfo.setMaximumStorageQuantity(calculateMaximumStorageQuantity(warehouseInfo));
        return warehouseInfo;
    }
    //曹科  根据仓库id查询
    @Override
    public WarehouseInfo queryWarehouseInfobyid(WarehouseInfo warehouseInfo) {
        return this.getById(warehouseInfo.getId());
    }
    //曹科  根据页面给的数据来修改
    @Override
    public Map amendWarehouseInfo(WarehouseInfo warehouseInfo) {
        Map map=new HashMap();
        map.put("code", 0);
        map.put("msg", "修改失败");
        if(warehouseInfo!=null){
            WarehouseInfo w=this.getById(warehouseInfo.getId());
            if(w!=null){
                boolean b=this.updateById(warehouseInfo);
                if(b){
                    map.put("code", 1);
                    map.put("msg", "修改成功");
                }
            }
        }
        return map;
    }
    // 曹科 根据id来删除 假删
    @Override
    public Map querybyidUpdateWarehouseInfo(Integer id) {
        Map map=new HashMap();
        map.put("code", 0);
        map.put("msg", "删除失败");
        if(id!=null) {
            WarehouseInfo w=this.getById(id);
            if(w!=null){
                //获取仓库详情并且循环检查当前库存和锁定库存是否清空
                QueryWrapper queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
                queryWrapper.eq("p_id",w.getId());
                List<WarehouseDetailsInfo> list=this.list(queryWrapper);
                int num=0;
                for(WarehouseDetailsInfo wd : list){
                    num+=wd.getAmount()+wd.getLockStock();
                }
                if(num==0){
                    w.setIsDelete(1);
                    boolean b=this.updateById(w);
                    if(b){
                        map.put("code", 1);
                        map.put("msg", "删除成功");
                    }
                }
            }
        }
        return map;
    }

    @Override
    public Map querybywarname(WarehouseInfo warehouseInfo) {
        Map map=new HashMap();
        map.put("code", 0);
        if(StringUtils.isNotEmpty(warehouseInfo.getWarName())){
            QueryWrapper<WarehouseInfo> queryWrapper=new QueryWrapper<WarehouseInfo>();
            queryWrapper.eq("war_name",warehouseInfo.getWarName());
            queryWrapper.eq("is_delete","0");
            WarehouseInfo w=getOne(queryWrapper);
            if(w!=null){
                map.put("code", 1);
            }
        }
        return map;
    }

    //根据商品id查询总数量
    @Override
    public Map queryUpNumByShopId(Integer id) {
        Integer num=0;
        Map map =new HashMap();
        map.put("code",0);
        map.put("msg","仓库无库存");

        QueryWrapper<WarehouseDetailsInfo> queryWrapper=
                new QueryWrapper<WarehouseDetailsInfo>();
        queryWrapper.eq("shop_id",id);
        queryWrapper.eq("is_check",1);
        List<WarehouseDetailsInfo> list = warehouseDetailsInfoMapper.selectList(queryWrapper);
        for (WarehouseDetailsInfo o : list) {
            num+=o.getAmount();
        }
        if (num>0){
            map.put("code",1);
            map.put("num",num);
        }
        return map;
    }

    @Override
    public Page queryAllWarehouseInfo(WarehouseInfo warehouseInfo, Integer pageno, Integer pagesize) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("is_check","1");
        queryWrapper.eq("is_delete","0");
        return this.page(new Page<WarehouseInfo>(pageno,pagesize),queryWrapper);
    }


    public Integer calculateRemnantInventory(WarehouseInfo warehouseInfo){
        //计算剩余库存
        Integer remnantInventory=0;
        QueryWrapper queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
        queryWrapper.eq("p_id",warehouseInfo.getId());
        List<WarehouseDetailsInfo> warehouseDetailsInfoList=warehouseDetailsInfoMapper.selectList(queryWrapper);
        //循环仓库详情数据
        if(warehouseDetailsInfoList.size()>0){
            for(WarehouseDetailsInfo w  : warehouseDetailsInfoList){
                remnantInventory+=w.getLockStock();
            }
            //循环结束，计算仓库最大库存【-】仓库详情表当前库存总数
            remnantInventory=warehouseInfo.getWarMaxStock()-remnantInventory;
        }else {
            remnantInventory=warehouseInfo.getWarMaxStock();
        }
        return remnantInventory;
    }

    public Integer calculateMaximumStorageQuantity(WarehouseInfo warehouseInfo){
        //计算最大可分配商品库存数量
        Integer maximumStorageQuantity=0;
        QueryWrapper queryWrapper=new QueryWrapper<WarehouseDetailsInfo>();
        queryWrapper.eq("p_id",warehouseInfo.getId());
        List<WarehouseDetailsInfo> warehouseDetailsInfoList=warehouseDetailsInfoMapper.selectList(queryWrapper);
        //循环仓库详情数据
        if(warehouseDetailsInfoList.size()>0){
            for(WarehouseDetailsInfo w  : warehouseDetailsInfoList){
                maximumStorageQuantity+=w.getMaxNum();
            }
            //循环结束，计算仓库最大库存【-】该仓库详情表的所有最大存储值
            maximumStorageQuantity=warehouseInfo.getWarMaxStock()-maximumStorageQuantity;
        }else {
            maximumStorageQuantity=warehouseInfo.getWarMaxStock();
        }
        return maximumStorageQuantity;
    }
}
